package com.jzo2o.customer.service.impl;



import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.expcetions.ForbiddenOperationException;
import com.jzo2o.common.model.CurrentUserInfo;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.customer.mapper.AgencyCertificationAuditMapper;
import com.jzo2o.customer.model.domain.AgencyCertificationAudit;
import com.jzo2o.customer.model.dto.request.AgencyCertificationAuditAddReqDTO;
import com.jzo2o.customer.model.dto.request.AgencyCertificationAuditPageQueryReqDTO;
import com.jzo2o.customer.service.IAgencyCertificationAuditService;
import com.jzo2o.mvc.utils.UserContext;
import com.jzo2o.mysql.utils.PageHelperUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 * 机构认证审核表 服务实现类
 * </p>
 *
 * @author itlxc
 * @since 2024-10-14
 */
@Service
@RequiredArgsConstructor
public class AgencyCertificationAuditServiceImpl extends ServiceImpl<AgencyCertificationAuditMapper, AgencyCertificationAudit> implements IAgencyCertificationAuditService {


    @Override
    public void submit(AgencyCertificationAuditAddReqDTO dto) {
        Long userId = UserContext.currentUserId();
        if (ObjectUtil.isNull(userId)) {
            throw new ForbiddenOperationException("请登录后再操作");
        }
        // 判断身份证号是否重复
        AgencyCertificationAudit agencyCertificationAudit = lambdaQuery().eq(AgencyCertificationAudit::getIdNumber, dto.getIdNumber()).one();
        if (ObjectUtil.isNotNull(agencyCertificationAudit)) {
            throw new ForbiddenOperationException("您已经提交过认证申请");
        }
        agencyCertificationAudit = new AgencyCertificationAudit();
        BeanUtil.copyProperties(dto, agencyCertificationAudit);
        agencyCertificationAudit.setServeProviderId(userId);
        boolean flag = this.save(agencyCertificationAudit);
        if (!flag) {
            throw new ForbiddenOperationException("提交认证申请失败");
        }
    }

    @Override
    public String getRejectReason() {
        Long userId = UserContext.currentUserId();
        if (ObjectUtil.isNull(userId)) {
            throw new ForbiddenOperationException("请登录后再操作");
        }
        return this.lambdaQuery()
                .eq(AgencyCertificationAudit::getServeProviderId, userId)
                .one().getRejectReason();
    }


    @Override
    public PageResult<AgencyCertificationAudit> pageQuery(AgencyCertificationAuditPageQueryReqDTO dto) {
        return PageHelperUtils.selectPage(dto,
                () -> this.lambdaQuery()
                        .eq(ObjectUtil.isNotNull(dto.getAuditStatus()), AgencyCertificationAudit::getAuditStatus, dto.getAuditStatus())
                        .eq(ObjectUtil.isNotNull(dto.getCertificationStatus()), AgencyCertificationAudit::getCertificationStatus, dto.getCertificationStatus()).eq(ObjectUtil.isNotNull(dto.getAuditStatus()), AgencyCertificationAudit::getAuditStatus, dto.getAuditStatus())
                        .like(StrUtil.isNotBlank(dto.getLegalPersonName()), AgencyCertificationAudit::getLegalPersonName, dto.getLegalPersonName())
                        .like(StrUtil.isNotBlank(dto.getName()), AgencyCertificationAudit::getName, dto.getName())
                        .list()
        );
    }

    @Override
    public void audit(Long id, Integer certificationStatus, String rejectReason) {
        CurrentUserInfo currentUserInfo = UserContext.currentUser();
        if (ObjectUtil.isNull(currentUserInfo)) {
            throw new ForbiddenOperationException("请登录后再操作");
        }
        AgencyCertificationAudit agencyCertificationAudit = this.getById(id);
        if (ObjectUtil.isNull(agencyCertificationAudit)) {
            throw new ForbiddenOperationException("该机构认证信息不存在");
        }
        agencyCertificationAudit.setCertificationStatus(certificationStatus);
        agencyCertificationAudit.setRejectReason(rejectReason);
        agencyCertificationAudit.setAuditStatus(1);
        agencyCertificationAudit.setAuditorId(currentUserInfo.getId());
        agencyCertificationAudit.setAuditorName(currentUserInfo.getName());
        agencyCertificationAudit.setAuditTime(LocalDateTime.now());
        boolean flag = this.updateById(agencyCertificationAudit);
        if (!flag) {
            throw new ForbiddenOperationException("审核机构认证信息失败");
        }
    }
}
